# -*- coding: utf-8 -*-
"""
Created on Tue Feb 27 08:46:49 2024

get PF coil current for given boundary

@author: xiangguchn@163.com
"""
import numpy as np
import math
import matplotlib.pyplot as plt

from scipy import special
from scipy.optimize import nnls

import multiprocessing as mp

from calculate_green_function import green_function as GF

class fix_boundary_slove():
    


    def __init__(self):
        self.machine_name = 'EXL-50U'
        self.machine_file = 'C:\\Users\\xiang\\Documents\\XINAO\\AI\\SEPY\\data\\EXL50U\\machine.xlsx'
        


        
    def get_green_function(self):
        
        green = {}
        green['mi_plasma_plasma'] = np.zeros(129*129)
        green['mi_plasma_pf'] = np.zeros(shape=(129*129, 15))
        
        self.green = green
        return green
    
    
    def get_dflux(self):
        
        dflux = np.zeros(10)
        return dflux
    
        
    def get_dPF(self):
        dPF = np.zeros(10)
        
        return dPF
    
    
    def get_init_plasma_para(self):
        
        
        plasma = np.zeros(10)
        
        return plasma
    
    
    
    def interate_dPF_with_dboundary(self, plasma):
        
        
        bs_plasma = self.get_boundary(plasma)
        
        dbs = self.bs - bs_plasma
        
        # find the corresponding PF current using least squares
        dPF = nnls(dbs, self.green['mi_plasma_pfs'])
        
        
        
        return dPF
    
    
    
    
    def interate_plasma_with_PFs(self):
        
        plasma = np.zeros(10)
        
        return plasma
    
    
    
    def fix_boundary_interate(self, bs, plasma, n_interate):
        
        
        
        plasma = self.get_init_plasma_para()
        PFs = np.zeros(10)        
        for i in range(n_interate):
            
            dPF = self.interate_dPF_with_dboundary(plasma)
            
            PFs = PFs + dPF
            
            plasma = self.interate_plasma_with_PFs(PFs)
        
        
        return PFs


        
if __name__ == '__main__':

    
    FBS = fix_boundary_slove()
    
    
    plasma = 500000
    bs = {'C':np.zeros(shape=(10,2)), 'C1':np.zeros(shape=(10,2)), }
    n_interate = 1
    
    flux_plasma, j_plasma = FBS.fix_boundary_interate(bs, plasma, n_interate)
    
    
    FBS.plot_flux(flux_plasma, islcfs=1)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    